﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///PromotionService 的摘要说明
/// </summary>
public class PromotionService
{
    private static PromotionService _instance = new PromotionService();
    private IPromotionDao _promotionDao = null;

	private PromotionService()
	{
        _promotionDao = new PromotionDao();
	}

    public static PromotionService GetInstance()
    {
        return _instance;
    }

    public IList<Promotion> GetPromotionList()
    {
        return _promotionDao.GetPromotionList();
    }
    
    public void AddPromotion(Promotion promotion)
    {
        try
        {
            _promotionDao.AddPromotion(promotion);
            CopyPromotionImage(promotion.PictureAName);
            CopyPromotionImage(promotion.PictureBName);
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\pictures\tmppromotion\"));
            List<System.IO.FileInfo> filelist = new List<System.IO.FileInfo>(dir.GetFiles());
            foreach (System.IO.FileInfo i in filelist)
            {
                System.IO.File.Delete(i.FullName);
            }
        }
        catch (Exception ex)
        {

        }
    }

    public void CopyPromotionImage(string imagename)
    {
        string tmpdestFilePath = HttpContext.Current.Server.MapPath(@"~\pictures\tmppromotion\") + imagename;
        string destFilePath = HttpContext.Current.Server.MapPath(@"~\pictures\promotion\") + imagename;
        string smalldestFilePath = HttpContext.Current.Server.MapPath(@"~\pictures\promotion\small") + imagename;
        System.IO.File.Copy(tmpdestFilePath, destFilePath);
        PictureHelper.MakeThumbnail(destFilePath, smalldestFilePath, 105, 105, "Cut");
    }

    public void DeletePromotion(string promotionId)
    {
        try
        {
            Promotion promotion = _promotionDao.GetPromotionById(promotionId);
            _promotionDao.DeletePromotion(promotionId);

            string pictureAName = HttpContext.Current.Server.MapPath(@"~\pictures\promotion\") + promotion.PictureAName;
            string pictureBName = HttpContext.Current.Server.MapPath(@"~\pictures\promotion\") + promotion.PictureBName;
            System.IO.File.Delete(pictureAName);
            System.IO.File.Delete(pictureBName);

            string smallpictureAName = HttpContext.Current.Server.MapPath(@"~\pictures\promotion\small") + promotion.PictureAName;
            string smallpictureBName = HttpContext.Current.Server.MapPath(@"~\pictures\promotion\small") + promotion.PictureBName;
            System.IO.File.Delete(smallpictureAName);
            System.IO.File.Delete(smallpictureAName);
        }
        catch (Exception ex)
        {

        }
    }

    public IList<Promotion> GetOrderedPromotionList()
    {
        return _promotionDao.GetOrderedPromotionList();
    }
}
